<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport"
	content="width=device-width, minimum-scale=1, initial-scale=1, maximum-scale=1, user-scalable=no"
	charset="UTF-8">
<link rel="stylesheet" href="android_asset://css/style.css" />
<link rel="stylesheet" href="android_asset://css/font.css" />
<title>添加巡检设备</title>
<style type="text/css">
   
</style>
</head>
<body>
	<div class="inspection" >
		<form id="form" action="/tickets/{{ticketId}}/saveParts">
		<input type="hidden" id="ticketid" name="ticketid" value="{{ticketId}}">
			   	<div class="form-group leo listing">
					<p>设备信息</p>
					<ul class="addDevice">
						<li><span class='icon-add' onclick="addPartMenu()"></span></li>
					</ul>
					<i class="right">点击加号添加</i>
				</div>
		</form>
		<div class="confirm_import" style="position:fixed;">
			<button type="button" class="but"><a href="javascript:void(0);">提交</a></button>
		</div>
	</div>
</body>
<script src="android_asset://jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="android_asset://jquery-migrate-1.2.1.min.js" type="text/javascript"></script>
<script src="android_asset://bridge.js" type="text/javascript"></script>
<script src="android_asset://android.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	  $('button.but').on('keydown touchstart', function () {
		  $(this).css("background", '#216cb9');
	  });
	  $('button.but').on('keyup touchend', function () {
	      $(this).css("background", '#3e8ddd');
	  });
});
var attId=0;
//添加设备
function addPartMenu(){
	var addCount = $(".addDevice img").length+($(".addDevice span").length-1);
	if(addCount>=8){
		 msAndroid.toast("添加设备的记录不能超过8条");
		 return;
	}
	partMenu('.addDevice',4)
}
//加载图片
function partMenu(picker,attType){
	if(msAndroid[picker]==null||msAndroid[picker]<0){
		msAndroid[picker]=0
	}
	var menu=[{text:"拍照录入",index:0},
	          {text:"手动录入",index:1}
	         ];
	msAndroid.menu(menu,function(response){
		var obj=eval('('+response+')');
		if(obj.status==1){
			if(obj.data.index==0){
				msAndroid.picture(picker,attType,null,false);
			}else if(obj.data.index==1){
				msAndroid.get("/tickets/deviceInput?picker="+picker,true);
			}
		}else{
			toast(obj.message);
		}
	},msAndroid[picker]);
}
//添加节点
function deviceInput(obj){
	var name=obj.picker.replace("\.","");
	var objStr="";
	for(var key in obj){
		objStr+="&"+key+"="+obj[key];
	}
	var li="<li id='att"+attId+"' style='float: left;width:5em;' class='listing_li' onclick=msAndroid.get('/tickets/deviceInfo?attId=att"+attId+objStr+"',true) >"+
	       "<span class='icon-device'></span><b>"+obj.name+"</b><input type='hidden' name='"+name+"' value='"+JSON.stringify(obj)+"'></li>";
	$(obj.picker).append(li);
	$(obj.picker).find('i.right').remove();
    attId++;
}
//删除节点
function delDevice(id){
	$("#"+id.attId).remove();
}

$(function(){
	//表单提交及校验   
	$(".confirm_import button").click(function(){
		var flag=false;
		var addCount = $(".addDevice img").length+($(".addDevice span").length-1);
		if(flag){
			 return;
		}
		//校验设备信息
		if(addCount==0){
			 msAndroid.toast("请添加设备信息");
			 return;
		}
		if(addCount>8){
			 msAndroid.toast("设备信息的记录不能超过8条");
			 return;
		}
		
		var formJson=msAndroid.changedTicketVO("#form");
		var url=$("#form").attr("action");
		var attJson=msAndroid.attVO("#form");

		if(attJson.original!=null){
			msAndroid.post("/tickets/{{ticketId}}/attachments/info",attJson,"post");
		}
		var btn = this;
		msAndroid.post(url,formJson,"post","history",function() {
			btn.disabled = false;
		});
		//禁止页面二次提交
	 	btn.disabled = true;
	});
});
</script>
</html>